﻿using ThresholdAssistant.Autofac;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace ThresholdAssistant.Http
{
    /// <summary>
    /// 发送钉钉通知工具类
    /// </summary>
    public class DingDingSendUtil : IBaseAutofac
    {
        /// <summary>
        /// 
        /// </summary>
        public PollyClientHelp PollyClientHelp { get; set; }

        /// <summary>
        /// 钉钉推送调用自定义机器人发Txt消息 
        /// </summary>
        /// <param name="url">接口地址</param>
        /// <param name="message">消息文案内容</param>
        /// <param name="isAt">是否@所有人</param>
        /// <param name="atMobiles">@部分人，格式：["1308986666","13109876543422"]</param>
        /// <returns></returns>
        public string SendMessage(string url, string message, bool isAt, IEnumerable<string> atMobiles)
        {
            var param = new
            {
                msgtype = "text",
                text = new { content = message },
                at = new { atMobiles, isAtAll = isAt }
            };
            return PostAsync(url, param).Result;
        }

        /// <summary>
        /// 发送Post请求
        /// </summary>
        /// <typeparam name="R"></typeparam>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="paramters"></param>
        /// <param name="headers"></param>
        /// <param name="contentType"></param>
        /// <param name="timeoutSeconds">超时时间(单位：秒)</param>
        /// <returns></returns>
        private async Task<string> PostAsync<R>(string url, R paramters, Dictionary<string, string> headers = null, string contentType = "application/json", int timeoutSeconds = 3)
        {
            return await PollyClientHelp.PostAsync(url, paramters, headers, contentType, timeoutSeconds);
        }
    }
}